package com.cmc6.request.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cmc6.common.model.dto.ChangeBoolByStrId;
import com.cmc6.common.model.entity.EsErrorDO;
import com.cmc6.common.util.ApiResult;
import com.cmc6.request.model.dto.RequestAnalysisRequestNumberDTO;
import com.cmc6.request.model.dto.RequestErrorLogPageDTO;
import com.cmc6.request.model.dto.RequestPageDTO;
import com.cmc6.request.model.vo.RequestAnalysisRequestNumberVO;
import com.cmc6.request.model.vo.RequestPageVO;
import com.cmc6.request.model.vo.RequestRankingVO;
import com.cmc6.request.service.RequestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/request")
@Api(tags = "接口请求记录")
public class RequestController {

    @Resource
    RequestService baseService;

    @PreAuthorize("hasAuthority('request:page')")
    @PostMapping("/page")
    @ApiOperation(value = "分页排序查询 接口请求记录")
    public ApiResult<Page<RequestPageVO>> myPage(@RequestBody @Valid RequestPageDTO dto) {
        return ApiResult.ok(baseService.myPage(dto));
    }

    @PreAuthorize("hasAuthority('request:page')")
    @PostMapping("/analysisRequestNumber")
    @ApiOperation(value = "统计请求数")
    public ApiResult<List<RequestAnalysisRequestNumberVO>> analysisRequestNumber(
        @RequestBody @Valid RequestAnalysisRequestNumberDTO dto) {
        return ApiResult.ok(baseService.analysisRequestNumber(dto));
    }

    @PreAuthorize("hasAuthority('request:page')")
    @PostMapping("/ranking")
    @ApiOperation(value = "请求总数排行")
    public ApiResult<List<RequestRankingVO>> ranking() {
        return ApiResult.ok(baseService.ranking());
    }

    @PreAuthorize("hasAuthority('request:page')")
    @PostMapping("/slowRanking")
    @ApiOperation(value = "慢请求排行")
    public ApiResult<List<RequestRankingVO>> slowRanking() {
        return ApiResult.ok(baseService.slowRanking());
    }

    @PreAuthorize("hasAuthority('request:page')")
    @PostMapping("/analyzeAll")
    @ApiOperation(value = "分析所有请求")
    public ApiResult<RequestRankingVO> analyzeAll(@RequestBody @Valid RequestPageDTO dto) {
        return ApiResult.ok(baseService.analyzeAll(dto));
    }

    @PreAuthorize("hasAuthority('request:page')")
    @PostMapping("/errorLogPage")
    @ApiOperation(value = "分页排序查询 请求错误日志")
    public ApiResult<Page<EsErrorDO>> errorLogPage(@RequestBody @Valid RequestErrorLogPageDTO dto) {
        return ApiResult.ok(baseService.errorLogPage(dto));
    }

    @PreAuthorize("hasAuthority('request:insertOrUpdate')")
    @PostMapping("/changeErrorLogReadFlag")
    @ApiOperation(value = "已读/未读 请求错误日志")
    public ApiResult<String> changeErrorLogReadFlag(@RequestBody @Valid ChangeBoolByStrId changeBoolByStrId) {
        return ApiResult.ok(baseService.changeErrorLogReadFlag(changeBoolByStrId));
    }

}
